import { defineStore } from 'pinia';
import { updateAuthInfoApi } from 'src/api/auth';
import { getAuthInfoApi, getInfoApi, getUserInfoApi, loginApi } from 'src/api/user';
import { getUrlParam } from 'src/boot/paramhandler';
import { showLoadingToast } from 'vant';

export type UserAuthInfo = {
  idcard: string;
  authFlag: number;
  realname: string;
};
export type UserInfo = {
  token: string;
  channelCode: string;
  channelName: string;
  regChannelCode: string;
  regChannelName: string;
  uid: string;
  username: string;
  mobile: string;
  authFlag: number;
  faceFlag: number;
  sourceFlag: number;
  limitAmount: number;
  userAuthInfo: UserAuthInfo;
};

export const useAppStore = defineStore('app', {
  state: () => {
    const { param } = getUrlParam()
    return {
      token: sessionStorage.getItem('token') ?? localStorage.getItem('token') ?? '',
      channelCode: sessionStorage.getItem('channelCode') || localStorage.getItem('channelCode')  || `${param.channelCode}` || '',
      userInfo: {} as UserInfo,
      mobile: sessionStorage.getItem('mobile') ?? localStorage.getItem('token_mobile') ?? '',
      smsFlag: sessionStorage.getItem('smsFlag') ?? 0,
      channelName: sessionStorage.getItem('channelName') ?? 0,
      pageType: (localStorage.getItem('pageType') || 'h5_auth_list') as 'h5_auth_list' | 'h5_auth_download' | 'list' | 'download' | 'auth',
      chargeType: undefined as number | undefined,
    }
  },
  getters: {
    isLogin: (state) => !!state.token,
    isSMS: (state) => +state.smsFlag == 1,
    isAuth: (state) => state.userInfo.authFlag == 1,
    getAuthInfoData: (state) => {
      const info = state.userInfo.userAuthInfo ?? {}
      const tempInfo = {
        idcard: info.idcard || sessionStorage.getItem('idcard'),
        realname: info.realname || sessionStorage.getItem('realname')
      }
      const authFlag = tempInfo.idcard && tempInfo.realname ? 1 : 0
      return {
        authFlag,
        ...tempInfo
      }
    }
  },
  actions: {
    async updateAuthInfo(idcard: string, realname: string) {
      await updateAuthInfoApi({
        idcard: idcard,
        realname: realname,
      });
      sessionStorage.setItem('idcard', idcard);
      sessionStorage.setItem('realname', realname);
      // this.userInfo.authFlag = 1
      await this.getUserInfo()
    },
    async login(phone: string, code?: string) {
      const res = await loginApi({ loginType: code ? 0 : 3, mobile: phone, loginCode: code });
      this.token = res.data.token;
      sessionStorage.setItem('mobile', phone);
      sessionStorage.setItem('token', res.data.token);
      await this.getUserInfo();
    },
    async jointLogin(phone: string,){
      const res = await loginApi({ loginType: 7, mobile: phone,  }, { diyError: true });
      const resUserInfo = await getUserInfoApi({token: res.data.token}, { diyError: true });
      this.token = res.data.token;
      sessionStorage.setItem('mobile', phone);
      sessionStorage.setItem('token', res.data.token);
      await this.setUserInfo(resUserInfo.data);
    },
    async setUserInfo(_userInfo: UserInfo) {
      const userInfo = {} as UserInfo;
      userInfo.token = _userInfo?.['token'] ?? '';
      userInfo.uid = _userInfo?.['uid'] ?? '';
      userInfo.regChannelName = _userInfo?.['regChannelName'] ?? '';
      userInfo.regChannelCode = _userInfo?.['regChannelCode'] ?? '';
      userInfo.regChannelCode = _userInfo?.['regChannelCode'] ?? '';
      userInfo.authFlag = _userInfo?.['authFlag'] ?? 0;
      userInfo.channelCode = _userInfo?.['channelCode'] ?? '';
      userInfo.username = _userInfo?.['username'] ?? '';
      userInfo.mobile = _userInfo?.['mobile'] ?? '';
      userInfo.sourceFlag = _userInfo?.['sourceFlag'] ?? 0;
      userInfo.limitAmount = _userInfo?.['limitAmount'] ?? 0;
      this.userInfo = userInfo;
      if (userInfo.authFlag == 1) {
        await this.getAuthInfo();
      }
    },
    async getUserInfo() {
      const res = await getUserInfoApi();
      this.setUserInfo(res.data)
    },
    async getAuthInfo() {
      const res = await getAuthInfoApi();
      const userAuthInfo = {} as UserAuthInfo;
      userAuthInfo.authFlag = res.data?.['authFlag'] ?? 0;
      userAuthInfo.idcard = res.data?.['idcard'] ?? '';
      userAuthInfo.realname = res.data?.['realname'] ?? '';
      this.userInfo.userAuthInfo = userAuthInfo;
    },
    async initPage() {
      const res = await getInfoApi<{
        smsFlag: string| number
        channelName: string
        pageType: "download" | "h5_auth_list" | "h5_auth_download" | "list" | "auth"
        chargeType: number
        channelCode: string
      }>();
      const { smsFlag, channelName, pageType, chargeType } = res.data;
      this.smsFlag = smsFlag;
      this.channelName = channelName;
      this.pageType = pageType;
      this.chargeType = +chargeType;
      sessionStorage.setItem('smsFlag', `${smsFlag}`);
      sessionStorage.setItem('channelName', channelName);
      sessionStorage.setItem('chargeType', `${chargeType}`);
      return res.data
    },
  },
});

